﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;

namespace DC.Views
{

    public partial class Form_ProjectInfo : Skin_DevExpress
    {
        Utils.Database DB = Utils.Global.GlobalDB;
        public Form_ProjectInfo()
        {
            InitializeComponent();
        }

        private void Form_ProjectInfo_Load(object sender, EventArgs e)
        {
            loadProjectInfo();
            loadFatherList();
            loadProjectSuffix();
            loadType();
        }
        private int IDget;                      //0为新增项目  其他为正常ID;
        public int Lltrans
        {
            get
            {
                return IDget;
            }
            set
            {
                IDget = value;
            }
        }
        private void loadType()
        {
            //加载类型下拉框
            Type.Items.Clear();
            DataSet TypeList = DB.GetDs("select  distinct(Project_Type) from Project");
            int Type_Rows = TypeList.Tables[0].Rows.Count;
            for (int i = 0; i < Type_Rows; i++)
            {
                Type.Items.Add(TypeList.Tables[0].Rows[i][0].ToString());
            }
        }
        private void loadProjectSuffix()
        {
            //加载单位下拉框
            comboBox2.Items.Clear();
            DataSet SuffixList = DB.GetDs("select  distinct(Project_Suffix) from Project");
            int Suffix_Rows = SuffixList.Tables[0].Rows.Count;
            for (int i = 0; i < Suffix_Rows; i++)
            {
                comboBox2.Items.Add(SuffixList.Tables[0].Rows[i][0].ToString());
            }
        }
        private void loadFatherList()
        {
            //加载组别下拉框
            comboBox1.Items.Clear();
            DataSet FatherList = DB.GetDs("select  distinct(Project_Father) from Project");
            int Father_Rows = FatherList.Tables[0].Rows.Count;
            for (int i = 0; i < Father_Rows; i++)
            {
                comboBox1.Items.Add(FatherList.Tables[0].Rows[i][0].ToString());
            }


        }
        private void loadProjectInfo()
        {
            // 加载项目信息
            if (IDget == 0)
            {
                skinLabel8.Text = "新增项目";
            }
            else
            {
                skinLabel8.Text = IDget.ToString();
                DataRow currentProject = DB.GetDs("select * from Project where Project_ID=" + IDget).Tables[0].DefaultView[0].Row;
                textBox1.Text = currentProject["Project_Name"].ToString();
                textBox3.Text = currentProject["Project_Price"].ToString();
                comboBox2.Text = currentProject["Project_Suffix"].ToString();
                Type.Text = currentProject["Project_Type"].ToString();
                comboBox1.Text = currentProject["Project_Father"].ToString();
                textBox2.Text = currentProject["Project_Abbreviation"].ToString();

            }
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            if (IDget == 0)
            {
                AddNewProject();
            }
            else
            {
                UpdateProject();
            }
        }

        private void UpdateProject()
        {
            if (textBox1.Text.Trim() == String.Empty || textBox3.Text.Trim() == String.Empty || textBox2.Text.Trim() == String.Empty)
            {
                MessageBox.Show("有信息未输入");
            }
            else
            {
                string sql = @"update Project set Project_Name ='" + textBox1.Text +
                                             "',Project_Price ='" + textBox3.Text +
                                             "',Project_Suffix ='" + comboBox2.Text +
                                             "',Project_Type ='" + Type.Text +
                                             "',Project_Father ='" + comboBox1.Text +
                                             "',Project_Abbreviation ='" + textBox2.Text + "' where Project_ID =" + IDget;
                DB.Execute(sql);
                MessageBox.Show("修改成功！", "修改成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                loadProjectInfo();
            }
        }
        private void AddNewProject()
        {
            if (textBox1.Text.Trim() == String.Empty || textBox3.Text.Trim() == String.Empty || textBox2.Text.Trim() == String.Empty)
            {
                MessageBox.Show("有信息未输入");
                return;
            }
            string sql = string.Format(@"insert into Project(Project_Name, Project_Price, Project_Suffix, Project_Type,
                                        Project_Father,Project_Abbreviation)values(
                                                '{0}','{1}','{2}','{3}','{4}','{5}')",
                                                textBox1.Text,
                                                textBox3.Text,
                                                comboBox2.Text,
                                                    Type.Text,
                                                comboBox1.Text,
                                                textBox2.Text
                                                );
            DB.Execute(sql);
            MessageBox.Show("保存成功！", "保存成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (IDget == 0)
            {
                DialogResult r = MessageBox.Show("是否继续添加项目", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (r == DialogResult.Yes)
                {
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                    comboBox2.Text = "";
                    comboBox1.Text = "";
                    Type.Text = "";
                }
                else
                {
                    this.Close();
                }
            }

        }

        private void Father_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            _ = textBox2.Text = Utils.ChineseHelper.GetChineseSpell(textBox1.Text).ToUpper();
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ( (e.KeyChar < 48 || e.KeyChar > 57 ) && e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;
            }
        }
    }
}
